package com.david.auth.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author David
 * @desc 加密
 * @date 2022/4/18 10:05
 */
public class CryptoUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(CryptoUtils.class);

    private static final String ALGORITHM_MD5 = "MD5";
    private static final String ALGORITHM_SHA256 = "SHA-256";
    private static final String ALGORITHM_SHA1 = "SHA-1";
    private static final String CHAT_SET_UTF8 = "UTF-8";
    private static final String ENCODE_STRING_HEX = "hex";
    private static final String ENCODE_STRING_BASE64 = "base64";
    private static final String ENCODE_STRING_BASE58 = "base58";


    /**
     * 32个字符
     * @param str
     * @return
     */
    public static String encryptMd5Hex(String str) {
        return encryptHashFunction(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_HEX);
    }

    /**
     * 24个字符
     * @param str
     * @return
     */
    public static String encryptHd5Base64(String str) {
        return encryptHashFunction(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_BASE64);
    }

    /**
     * 22个字符
     * @param str
     * @return
     */
    public static String encryptMd5Base58(String str) {
        return encryptHashFunction(str, ALGORITHM_MD5, CHAT_SET_UTF8, ENCODE_STRING_BASE58);
    }

    /**
     * 40个字符
     * @param str
     * @return
     */
    public static String encryptSha1Hex(String str) {
        return encryptHashFunction(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_HEX);
    }


    /**
     * 28个字符
     * @param str
     * @return
     */
    public static String encryptSha1Base64(String str) {
        return encryptHashFunction(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_BASE64);
    }

    /**
     * 28个字符
     * @param str
     * @return
     */
    public static String encryptSha1Base58(String str) {
        return encryptHashFunction(str, ALGORITHM_SHA1, CHAT_SET_UTF8, ENCODE_STRING_BASE58);
    }

    /**
     * 64个字符
     * @param str
     * @return
     */
    public static String encryptSha256Hex(String str) {
        return encryptHashFunction(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_HEX);
    }

    /**
     * 44个字符
     * @param str
     * @return
     */
    public static String encryptSha256Base64(String str) {
        return encryptHashFunction(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_BASE64);
    }

    /**
     * 44个字符
     * @param str
     * @return
     */
    public static String encryptSha256Base58(String str) {
        return encryptHashFunction(str, ALGORITHM_SHA256, CHAT_SET_UTF8, ENCODE_STRING_BASE58);
    }


    private static String encryptHashFunction(String str, String algorithm, String chatset, String encodeMethod) {
        MessageDigest messageDigest;
        String encodeStr = "";
        try {
            messageDigest = MessageDigest.getInstance(algorithm);
            messageDigest.update(str.getBytes(chatset));

            byte[] digestBytes = messageDigest.digest();

            if (ENCODE_STRING_BASE64.equals(encodeMethod)) {
                encodeStr = byte2Base64(digestBytes);
            } else if (ENCODE_STRING_BASE58.equals(encodeMethod)) {
                encodeStr = Base58.encode(digestBytes);
            } else {
                encodeStr = byte2Hex(digestBytes);
            }

        } catch (Exception e) {
            LOGGER.error("", e);
        }
        return encodeStr;
    }


    private static String byte2Base64(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }


    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }


    /**
     * 用java原生的摘要实现SHA256加密
     *
     * @param str 加密前的报文
     * @return
     */
    public static byte[] getSha256(String str) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(str.getBytes("UTF-8"));
            return messageDigest.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }


}
